//
//  OcrPage.swift
//  ReportScan
//
//  Created by woniu on 2025/7/23.
//

import SwiftUI

struct OcrPage: View {
    private let recognService = ImageRecognizer()
    var body: some View {
            Text("提取图片中的文本")
            
            VStack(alignment: .leading) {
                Button(role: .none) {
                    choseOnImage()
                } label: {
                    Text("选择图片")
                }
            }
            .padding()
            Spacer()
    }
    
    private func choseOnImage(){
        let openPanel = NSOpenPanel()
        openPanel.title = "选择报告文件"
        openPanel.showsHiddenFiles = false
        openPanel.allowsMultipleSelection = false
        openPanel.canChooseDirectories = true
        openPanel.canCreateDirectories = true
        
        openPanel.begin {result in
            if result == .OK, let url = openPanel.url {
               recognizer(imagePath: url)
            }
        }
    }
    
    private func recognizer(imagePath:URL){
        guard let image = NSImage(contentsOf:imagePath) else {
            return
        }
        recognService.patternList = ["诊断医生","男"]
        recognService.recoginzeFinishBlock = {content in
            print("============>>>>找到了:\(content)")
        }
        recognService.startScan(image: image)
        
    }
}

#Preview {
    OcrPage()
}
